package com.proj.dao.impl;

import com.proj.dao.UserDao;
import com.proj.entities.UserEntity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class InMemoryDaoImpl implements UserDao
{
    private Map<String, UserEntity> users = new HashMap<String, UserEntity>()
    {
        {
            final UserEntity chandruEntity = new UserEntity();
            chandruEntity.setUserName("Chandru");
            chandruEntity.setFirstName("Chandrasekhar");
            chandruEntity.setLastName("Hebbasooru");
            chandruEntity.setAddress("Bangalore");
            chandruEntity.setId(1);
            chandruEntity.setAge(25);
            chandruEntity.setCity("Bangalore");
            chandruEntity.setEmail("chandru@ciso.com");
            chandruEntity.setPhone("984500000");
            chandruEntity.setPhone("984500000");
            chandruEntity.setCreatedOn(new Date());
            chandruEntity.setUpdatedOn(new Date());
            put(chandruEntity.getUserName(), chandruEntity);

            final UserEntity raviEntity = new UserEntity();
            raviEntity.setUserName("gravi");
            raviEntity.setFirstName("Ravi");
            raviEntity.setLastName("Gunupuree");
            raviEntity.setAddress("Bangalore");
            raviEntity.setId(2);
            raviEntity.setAge(26);
            raviEntity.setCity("Hyderabad");
            raviEntity.setEmail("ravi@ciso.com");
            raviEntity.setPhone("984590000");
            raviEntity.setCreatedOn(new Date());
            raviEntity.setUpdatedOn(new Date());
            put(raviEntity.getUserName(), raviEntity);

            final UserEntity dummyEntity = new UserEntity();
            dummyEntity.setUserName("dummy");
            dummyEntity.setFirstName("Dummy");
            dummyEntity.setLastName("User");
            dummyEntity.setAddress("Bangalore");
            dummyEntity.setId(3);
            dummyEntity.setAge(27);
            dummyEntity.setCity("Bangalore");
            dummyEntity.setEmail("dummy@ciso.com");
            dummyEntity.setPhone("9845770000");
            dummyEntity.setCreatedOn(new Date());
            dummyEntity.setUpdatedOn(new Date());
            put(dummyEntity.getUserName(), dummyEntity);

            final UserEntity user1Entity = new UserEntity();
            user1Entity.setUserName("user1");
            user1Entity.setFirstName("New");
            user1Entity.setLastName("User");
            user1Entity.setAddress("Bangalore");
            user1Entity.setId(3);
            user1Entity.setAge(27);
            user1Entity.setCity("Bangalore");
            user1Entity.setEmail("dummy@ciso.com");
            user1Entity.setPhone("9845770000");
            user1Entity.setCreatedOn(new Date());
            user1Entity.setUpdatedOn(new Date());
            put(user1Entity.getUserName(), user1Entity);
        }
    };


    public UserEntity getUser(String userName)
    {
        return users.get(userName);
    }

    public List<UserEntity> getAllUsers()
    {
        return new ArrayList<UserEntity>(users.values());
    }

    public long createUser(UserEntity user)
    {
        final int id = users.size() + 1;
        user.setId(id);
        final Date date = new Date();
        user.setCreatedOn(date);
        user.setUpdatedOn(date);
        users.put(user.getUserName(), user);
        return id;
    }

    public void updateUser(UserEntity user)
    {
        user.setUpdatedOn(new Date());
        users.put(user.getUserName(), user);
    }

    public void deleteUser(String userName)
    {
        users.remove(userName);
    }


}
